﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Handrise.CDCable.Mobile.Business
{
    public class UserService
    {
        private Synchronize synchronize;

        public UserService()
        {
            synchronize = new Synchronize();
            synchronize.OnLoginComplete += new CallWebServiceCompleteHandler(synchronize_OnLoginComplete);

        }

        public event LoginCompleteHandler OnLoginComplete;

        /// <summary>
        /// 调用登录完成的事件处理
        /// </summary>
        /// <param name="info"></param>
        /// <param name="isSuccess"></param>
        /// <param name="userData"></param>
        void synchronize_OnLoginComplete(string info, bool isSuccess, object userData)
        {
            User user = null;
            if (isSuccess)
            {
                //从调用成功
                user = (User)userData;
                DoLoginSuccess(user);
            }

            if (OnLoginComplete != null)
                OnLoginComplete(isSuccess, user);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns></returns>
        public bool Login(string userName, string password)
        {
            //判断当前网络是否连通
            if (AppLogic.netService.CurrentNetType == NetType.USB)
            {
                //通过网络进行登录
                synchronize.Login(userName, password);
            }
            else
            {
                //没有网络存在,本地登录
                User user = AppLogic.dataService.GetFirstEntity<User>("UserName ='" + userName + "' And Password = '" + password + "'", "");
                bool isSuccess = user != null;
                if (user != null)
                    DoLoginSuccess(user);

                if (OnLoginComplete != null)
                    OnLoginComplete(isSuccess, user);
            }
            return false;
        }

        /// <summary>
        /// 登录成功后的处理
        /// </summary>
        /// <param name="user"></param>
        private void DoLoginSuccess(User user)
        {
            user.LastLoginDateTime = DateTime.Now;

            //更新用户信息
            if (!AddUser(user))
                UpdateUser(user);

            //设置Applogic中的用户信息
            AppLogic.LoginUser = user;

            //更新服务器时间
            if (AppLogic.netService.CurrentNetType == NetType.USB)
            {
                Synchronize synchronize = new Synchronize();
                synchronize.SynchronizeDatetime();


            }
        }

        /// <summary>
        /// 获取最近登录的用户
        /// </summary>
        /// <returns></returns>
        public User GetLastLoginUser()
        {
            return AppLogic.dataService.GetFirstEntity<User>("","LastLoginDateTime");
        }

        /// <summary>
        /// 添加一个用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool AddUser(User user)
        {
            return AppLogic.dataService.Add<User>(user) == 1;
        }

        /// <summary>
        /// 更新一个用户信息
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public bool UpdateUser(User user)
        {
            return AppLogic.dataService.Update<User>(user) == 1;
        }

        /// <summary>
        /// 获取所有用户
        /// </summary>
        /// <returns></returns>
        public List<User> GetAllUser()
        {
            return AppLogic.dataService.Find<User>("", "LastLoginDateTime desc");
        }

        public delegate void LoginCompleteHandler(bool isSuccess, User user);
    }
}
